<?php

namespace SisesporteAdmin\Controller;

use SisesporteAdmin\Form\PreMatriculaForm;
use Zend\View\Model\ViewModel;
use Sisesporte\Auth\PessoaAuth;
use \Sisesporte\Utils\Utils;

class PreMatriculaController extends CrudController {

    protected $utils;

    public function __construct() {
        $this->entity = "Sisesporte\Entity\PreMatricula";
        $this->form = "SisesporteAdmin\Form\PreMatriculaForm";
        $this->service = "Sisesporte\Service\PreMatriculaService";
        $this->controller = "pre-matricula";
        $this->route = "sisesporte-admin";
        $this->utils = new Utils();
    }

    protected $msg;

    public function indexAction() {
        $pessoaAuth = new PessoaAuth();

        $data = $this->getEm()
                ->getRepository($this->entity)
                ->findBySqAluno($pessoaAuth->getLoggedUser()->sqPessoa);
        return new ViewModel(array('data' => $data));
    }

    public function newAction() {
        $form = new PreMatriculaForm($this->getEm());

        $request = $this->getRequest();
        if ($request->isPost()) {
//            echo "<pre>";
//            var_dump($request->getPost()->toArray());
//            echo "</pre>";exit;
            $array = $request->getPost();
            $array['stAtivo'] = TRUE;
            $service = $this->getServiceLocator()->get($this->service);

            $service->insert($request->getPost()->toArray());
                $this->msg = TRUE;
            
        }
        return new ViewModel(array('form' => $form, 'msg' => $this->msg));
    }

    public function viewAction() {
        $repository = $this->getEm()->getRepository("Sisesporte\Entity\PreMatricula");

        if ($this->params()->fromRoute('id', 0)) {
            $entity = $repository->find($this->params()->fromRoute('id', 0));
        }

        return new ViewModel(array('entity' => $entity, 'utils' => $this->utils));
    }

}
